package com.bjsxt.controller;

import com.bjsxt.dto.RolePrivilegesParam;
import com.bjsxt.model.Result;
import com.bjsxt.model.SysMenuVo;
import com.bjsxt.service.SysRolePrivilegeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * description:
 * 角色权限配置控制层
 * TODO: 这里的访问权限控制在书写的时候对应的权限表没有对应的权限, 需要后期加入, 权限访问还是要加的, 暂时注释掉
 *
 * @author ALIENWARE <br />
 * @date 2021/7/9
 */
@RestController
@Api(tags = "角色权限配置控制器")
public class SysRolePrivilegeController {

    /**
     * 角色权限业务层接口
     */
    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    /**
     * 查询角色的菜单权限列表
     *
     * @param roleId 角色 id
     * @return 统一响应对象
     */
    @GetMapping("/roles_privileges")
    //@PreAuthorize("hasAuthority('sys_role_query')")
    @ApiOperation(value = "查询角色的菜单权限列表", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色 id", dataType = "Long", required = true, paramType = "query", example = "1")
    })
    public Result<List<SysMenuVo>> findSysMenuAndPrivileges(@NotNull Long roleId) {
        //调用业务层方法进行数据的查询
        List<SysMenuVo> sysMenus = sysRolePrivilegeService.findSysMenuAndPrivileges(roleId);
        //进行数据的返回
        return Result.success(sysMenus);
    }

    /**
     * 更新角色的权限
     *
     * @param rolePrivilegesParam RolePrivilegesParam Json 对象
     * @return 统一响应对象
     */
    @PostMapping("/grant_privileges")
    //@PreAuthorize("hasAuthority('sys_role_query')")
    @ApiOperation(value = "更新角色的权限", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rolePrivilegesParam", value = "RolePrivilegesParam Json 数据", dataType = "String", paramType = "body", example = "{}")
    })
    public Result<Void> grantPrivileges(@RequestBody RolePrivilegesParam rolePrivilegesParam) {
        // 调用业务层方法进行权限的授予
        boolean flag = sysRolePrivilegeService.grantPrivileges(rolePrivilegesParam);
        // 如果为 true 授予成功
        if (flag) return Result.success("授予成功");
        // 如果为 false 授予失败
        return Result.fail("授予失败");
    }
}
